home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZRELBIT.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  279 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zrelbit.c */
  20. /* Relational, boolean, and bit operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "dict.h"
  25. #include "store.h"
  26.  
  27. /* Forward references */
  28. int obj_le(P2(os_ptr, os_ptr));
  29.  
  30. /* <obj1> <obj2> eq <bool> */
  31. int
  32. zeq(register os_ptr op)
  33. {    register os_ptr op1 = op - 1;
  34. #define eq_check_read(opp, dflt)\
  35.   switch ( r_type(opp) )\
  36.    {    case t_string: case t_array: case t_mixedarray: case t_shortarray:\
  37.       check_read(*opp); break;\
  38.     case t_dictionary: check_dict_read(*opp); break;\
  39.     default: dflt; break;\
  40.    }
  41.     eq_check_read(op1, check_op(2));
  42.     eq_check_read(op, 0);
  43.     make_bool(op1, (obj_eq(op1, op) ? 1 : 0));
  44.     pop(1);
  45.     return 0;
  46. }
  47.  
  48. /* <obj1> <obj2> ne <bool> */
  49. int
  50. zne(register os_ptr op)
  51. {    /* We'll just be lazy and use eq. */
  52.     int code = zeq(op);
  53.     if ( !code ) op[-1].value.index ^= 1;
  54.     return code;
  55. }
  56.  
  57. /* <num1> <num2> ge <bool> */
  58. /* <str1> <str2> ge <bool> */
  59. int
  60. zge(register os_ptr op)
  61. {    int code = obj_le(op, op - 1);
  62.     if ( code < 0 ) return code;
  63.     make_bool(op - 1, code);
  64.     pop(1);
  65.     return 0;
  66. }
  67.  
  68. /* <num1> <num2> gt <bool> */
  69. /* <str1> <str2> gt <bool> */
  70. int
  71. zgt(register os_ptr op)
  72. {    int code = obj_le(op - 1, op);
  73.     if ( code < 0 ) return code;
  74.     make_bool(op - 1, code ^ 1);
  75.     pop(1);
  76.     return 0;
  77. }
  78.  
  79. /* <num1> <num2> le <bool> */
  80. /* <str1> <str2> le <bool> */
  81. int
  82. zle(register os_ptr op)
  83. {    int code = obj_le(op - 1, op);
  84.     if ( code < 0 ) return code;
  85.     make_bool(op - 1, code);
  86.     pop(1);
  87.     return 0;
  88. }
  89.  
  90. /* <num1> <num2> lt <bool> */
  91. /* <str1> <str2> lt <bool> */
  92. int
  93. zlt(register os_ptr op)
  94. {    int code = obj_le(op, op - 1);
  95.     if ( code < 0 ) return code;
  96.     make_bool(op - 1, code ^ 1);
  97.     pop(1);
  98.     return 0;
  99. }
  100.  
  101. /* <num1> <num2> max <num> */
  102. /* <str1> <str2> max <str> */
  103. int
  104. zmax(register os_ptr op)
  105. {    int code = obj_le(op - 1, op);
  106.     if ( code < 0 ) return code;
  107.     if ( code )
  108.        {    ref_assign(op - 1, op);
  109.        }
  110.     pop(1);
  111.     return 0;
  112. }
  113.  
  114. /* <num1> <num2> min <num> */
  115. /* <str1> <str2> min <str> */
  116. int
  117. zmin(register os_ptr op)
  118. {    int code = obj_le(op - 1, op);
  119.     if ( code < 0 ) return code;
  120.     if ( !code )
  121.        {    ref_assign(op - 1, op);
  122.        }
  123.     pop(1);
  124.     return 0;
  125. }
  126.  
  127. /* <bool1> <bool2> and <bool> */
  128. /* <int1> <int2> and <int> */
  129. int
  130. zand(register os_ptr op)
  131. {    check_type(op[-1], r_type(op));
  132.     switch ( r_type(op) )
  133.        {
  134.     case t_boolean:
  135.         op[-1].value.index &= op->value.index;
  136.         break;
  137.     case t_integer:
  138.         op[-1].value.intval &= op->value.intval;
  139.         break;
  140.     default:
  141.         return_error(e_typecheck);
  142.        }
  143.     pop(1);
  144.     return 0;
  145. }
  146.  
  147. /* <bool> not <bool> */
  148. /* <int> not <int> */
  149. int
  150. znot(register os_ptr op)
  151. {    switch ( r_type(op) )
  152.        {
  153.     case t_boolean:
  154.         op->value.index = !op->value.index;
  155.         break;
  156.     case t_integer:
  157.         op->value.intval = ~op->value.intval;
  158.         break;
  159.     default:
  160.         return_error(e_typecheck);
  161.        }
  162.     return 0;
  163. }
  164.  
  165. /* <bool1> <bool2> or <bool> */
  166. /* <int1> <int2> or <int> */
  167. int
  168. zor(register os_ptr op)
  169. {    check_type(op[-1], r_type(op));
  170.     switch ( r_type(op) )
  171.        {
  172.     case t_boolean:
  173.         op[-1].value.index |= op->value.index;
  174.         break;
  175.     case t_integer:
  176.         op[-1].value.intval |= op->value.intval;
  177.         break;
  178.     default:
  179.         return_error(e_typecheck);
  180.        }
  181.     pop(1);
  182.     return 0;
  183. }
  184.  
  185. /* <bool1> <bool2> xor <bool> */
  186. /* <int1> <int2> xor <int> */
  187. int
  188. zxor(register os_ptr op)
  189. {    check_type(op[-1], r_type(op));
  190.     switch ( r_type(op) )
  191.        {
  192.     case t_boolean:
  193.         op[-1].value.index ^= op->value.index;
  194.         break;
  195.     case t_integer:
  196.         op[-1].value.intval ^= op->value.intval;
  197.         break;
  198.     default:
  199.         return_error(e_typecheck);
  200.        }
  201.     pop(1);
  202.     return 0;
  203. }
  204.  
  205. /* <int> <shift> bitshift <int> */
  206. int
  207. zbitshift(register os_ptr op)
  208. {    int shift;
  209.     check_type(op[-1], t_integer);
  210.     check_type(*op, t_integer);
  211.     if ( op->value.intval < -31 || op->value.intval > 31 )
  212.         op[-1].value.intval = 0;
  213.     else if ( (shift = op->value.intval) < 0 )
  214.         op[-1].value.intval = ((ulong)(op[-1].value.intval)) >> -shift;
  215.     else
  216.         op[-1].value.intval <<= shift;
  217.     pop(1);
  218.     return 0;
  219. }
  220.  
  221. /* ------ Initialization procedure ------ */
  222.  
  223. op_def zrelbit_op_defs[] = {
  224.     {"2and", zand},
  225.     {"2bitshift", zbitshift},
  226.     {"2eq", zeq},
  227.     {"2ge", zge},
  228.     {"2gt", zgt},
  229.     {"2le", zle},
  230.     {"2lt", zlt},
  231.     {"2max", zmax},
  232.     {"2min", zmin},
  233.     {"2ne", zne},
  234.     {"1not", znot},
  235.     {"2or", zor},
  236.     {"2xor", zxor},
  237.     op_def_end(0)
  238. };
  239.  
  240. /* ------ Internal routines ------ */
  241.  
  242. /* Compare two operands (both numeric, or both strings). */
  243. /* Return 1 if op[-1] <= op[0], 0 if op[-1] > op[0], */
  244. /* or a (negative) error code. */
  245. #define bcval(v1, rel, v2) (op1->value.v1 rel op->value.v2 ? 1 : 0)
  246. int
  247. obj_le(register os_ptr op1, register os_ptr op)
  248. {    switch ( r_type(op1) )
  249.       {
  250.       case t_integer:
  251.         switch ( r_type(op) )
  252.           {
  253.           case t_integer:
  254.               return bcval(intval, <=, intval);
  255.           case t_real:
  256.               return bcval(intval, <=, realval);
  257.           default:
  258.               return_error(e_typecheck);
  259.           }
  260.       case t_real:
  261.         switch ( r_type(op) )
  262.           {
  263.           case t_real:
  264.               return bcval(realval, <=, realval);
  265.           case t_integer:
  266.               return bcval(realval, <=, intval);
  267.           default:
  268.               return_error(e_typecheck);
  269.           }
  270.       case t_string:
  271.         check_read(*op1);
  272.         check_read_type(*op, t_string);
  273.         return (bytes_compare(op1->value.bytes, r_size(op1),
  274.                       op->value.bytes, r_size(op)) <= 0 ? 1 : 0);
  275.       default:
  276.           return_error(e_typecheck);
  277.       }
  278. }
  279.